
//Anulima Choudhary
// Roll No. 2010018

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{ 
             int data;
             struct node *next,*prev;
           }*start;



// append into the list
void append_list(int num)
{ struct node *new_node,*current,*temp;
  new_node=(struct node *)malloc(sizeof(struct node));
  new_node->data=num;
  new_node->next=NULL;
  
  if(start==NULL)
  {  start=new_node;
     current=new_node;
  }
  else
  { temp=start;
  while(temp->next!=NULL)
  {temp=temp->next;
  }
  temp->next=new_node;
  }
}

// displaying the list
void display (struct node *q)
  {
      q=start;
      if(q==NULL)
     {
       printf("empty list!!");
       return;
     }
       

      while(q!=NULL)
    {
      printf("%d   ",q->data);
      q=q->next;
    }
     
  }

// function to reverse a list
void reverse(struct node *q)
{
   q=start;
   struct node *m, *n,*l,*s;
   m=q;
   n=NULL;
  while(m!=NULL)
 {
   s=n;
   n=m;
  m=m->next;
  n->next=s;
 }
   q=n;
   start=q;
}
// to find the number of nodes
//used in pop function and insert function
int length()
 {
   struct node *n;
   int c=0;
   n=start;
    while(n!=NULL)
   {
     n=n->next;
     c++;
   }
  return(c);
 }

// to insert a node at a particular location
void insert(int loc, int num)
  {
     int i;
     struct node *temp,*t,*r;
     r=start;       
      if(loc >length()||loc <=0)
   {
         printf("insertion is not possible :");
            return;
   }
       else if (loc==1)
         {
           
           temp=(struct node *)malloc(sizeof(struct node));
           temp->data=num;     

       
            if(start==NULL)
         {
          start=temp;
          start->next=NULL;
         }

            else
         {
          temp->next=start;
          start=temp;
}
       return;
         }
      else
 {
       for(i=1;i<=loc;i++)
          {
             t=r;  
             r=r->next;
          }
         temp=(struct node *)malloc(sizeof(struct node));
         temp->data=num;
         t->next=temp;
         t=temp;
         t->next=r;
        return;
       }
}


//delete a node
//assuming the node starts from 1
void pop(int pos)
{ 

     struct node *temp = start;
     
     int i;

    if(pos <=0 || pos>length())
    {
        printf("ERROR: Node does not exist!\n");
     }
    else
     {
         if(pos==1)
           {
            start=start->next;
           }
         else
         {
            for(i = 1; i < pos-1; ++i)
            { 
                    temp = temp->next;
            }
            temp->next = temp->next->next;
        }
    }
  
}
//returns index of first occurence of x
//assuming node starts from 1
int index1(int x)
{ struct node *p;
  int i;
  int count=0;
  p=start;
  while((p->data!=x)&&(p!=NULL))
    
  {   count++;
      p=p->next;
      
   }
  count++;
  return(count);
}
 


void remove1(int num)
 {
     struct node *q;
     struct node *m;
     q=start;
     while(q!=NULL)
    {
       if(q->data==num)
       {
           if(q==start)
           {
              start=q->next;
              free(q);
            return; 
           }
           else
         {
           m->next=q->next;
           free(q);
           return;
         }
      }else
        {
           m=q;
          q= q->next;   
        }

}
}


// returns the number of times num appears in the linked list
int count(int num)
{ int c=0;
  struct node *temp;
  temp=start;
  while(temp!=NULL)
  { if(temp->data==num)
     { c++;
       temp=temp->next;
      }
    else
    { temp=temp->next;
     }
   } 
  
   return c;
} 

void extend(int size)
{
        int i;
	int num;
	for (i=1; i<=size;i++)
	{       printf(" enter element %d:",i);
                scanf("%d",&num);
		append_list(num);

	}
}
//insert a node at the begining
void insert_beg(int x)
{
   struct node *temp;
   temp=malloc(sizeof(struct node));
   temp->data=x;
   if ( start== NULL)
   {
         start=temp; 
         start->next=NULL;
   }
   else
   {
         temp->next=start;
         start=temp;
   }
}

void main()
 {
   int ch,num;
   
   struct node *n;
   do
   {
    printf("\n 1.append an element in the list->");
    printf("\n 2.display the list-> :");
    printf("\n 3.reverse->");
    printf("\n 4.insert->");
    printf("\n 5.count->");
    printf("\n 6.pop->");
    printf("\n 7.remove->");
    printf("\n 8.index->");
    printf("\n 9.extend->");
    printf("\n 10. insert a node at beginning->");
    printf("\n 11.exit!!!");
    printf("\n enter your choice:");
    scanf("%d",&ch);

    switch(ch)
    {
         
     case 1:     {  int num;
                    printf("\n PLEASE ENTER THE NUMBER :-");
                    scanf("%d",&num);
                    append_list(num);
                    break;
                  }

  
     case 2:
      		{
         
         	    printf("\n list-> : ");
         	    display(n);
                    break;
      		}

     case 3:   {
    
                    reverse(start);
                    display(start);
                    break;
                }
     case 4:   { int val,l;
      		printf("enter the value to be inserted :");
      		scanf("%d",&val);
      		printf("enter the location :");
      		scanf("%d",&l);
      		insert(l,val);
      		break;
                }
     case 5:    { int num;
                  printf(" enter the number to be counted:");
                  scanf("%d" ,&num);
                  printf("No. of times %d occurs=%d",num,count(num));
                 
                  break;
                }
     case 6:   {  int j;
                  printf("\n ENTER THE LOCATION WHOSE ELEMENT IS TO BE POPPED:");
                  scanf("%d",&j);
                  pop(j);
                  break;
                }
     case 7:  { int num;
                printf("enter the element to be removed:");
                scanf("%d",&num);
                remove1(num);
                break;
              }
     case 8:  { int num,e;
                printf("\nenter the number whose index you want to find:");
                scanf("%d",&num);
                e=index1(num);
                printf("\n index of %d is=%d",num,e);
                break;
               }
     case 9:  { int i;
                printf("Enter the number of elements to be appended in the original list :");
	        scanf("%d",&i);
                extend(i);
                break;
               }
     case 10:  {  int x;
                  printf(" \nenter the element to be inserted at the beginning of the list:");
                  scanf("%d", &x);
                  insert_beg(x);
                  break;
                }
     case 11:	exit(0);
     default:   printf("\n invalid input!!");
  }
 }while((ch!=11));
}

// **********************END******************************
